package com.artwork.controller;

import com.artwork.common.domain.AjaxResult;
import com.artwork.common.enums.JsonResultEnum;
import com.artwork.common.exception.JsonException;
import com.artwork.model.Notice;
import com.artwork.service.NoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author JoJo
 * @Data 2021/9/8$ 15:23$
 * @Description
 * @Param $
 * @Return $
 */
@RestController
@RequestMapping("/admin")
public class NoticeController {
    @Autowired
    NoticeService noticeService;

    /**
     * @desc 新增公告
     * @param notice
     * @return
     */
    @PostMapping("/notice/add")
    public AjaxResult addNotice(Notice notice){
        notice.setCreateTime(System.currentTimeMillis());
        noticeService.addNotice(notice);
        return AjaxResult.success();
    }

    /**
     * @desc 修改公告
     * @param notice
     * @return
     * @throws JsonException
     */
    @PostMapping("/notice/update")
    public AjaxResult updateNotice(Notice notice) throws JsonException {
        Notice notice1 = noticeService.getNoticeById(notice.getId());
        if (notice1 == null){
            throw new JsonException(JsonResultEnum.TARGET_NULL);
        }
        notice.setCreateTime(System.currentTimeMillis());
        noticeService.updateNotice(notice);
        return AjaxResult.success();
    }

    /**
     * @desc 修改公告状态
     * @param id
     * @param status
     * @return
     * @throws JsonException
     */
    @PostMapping("/notice/status")
    public AjaxResult updateNoticeStatus(int id, int status) throws JsonException {
        Notice notice = noticeService.getNoticeById(id);
        if (notice == null){
            throw new JsonException(JsonResultEnum.TARGET_NULL);
        }
        noticeService.pushNotice(id, status);
        return AjaxResult.success();
    }

    /**
     * @desc 删除公告
     * @param id
     * @return
     * @throws JsonException
     */
    @PostMapping("/notice/delete")
    public AjaxResult deleteNotice(int id) throws JsonException {
        Notice notice = noticeService.getNoticeById(id);
        if (notice == null){
            throw new JsonException(JsonResultEnum.TARGET_NULL);
        }
        noticeService.deleteNotice(id);
        return AjaxResult.success();
    }
}
